#!/bin/bash -x

# Updates MODULE inside go.mod if it is already present to version VERSION.

source `git rev-parse --show-toplevel`/internal/buildscripts/modules

set -e

if grep -q "$MODULE " go.mod; then
  go get "$MODULE"@"$VERSION"
fi

# If MODULE is "go.opentelemetry.io/collector" need to update additional $modules as well
if [ "$MODULE" == "go.opentelemetry.io/collector" ]; then
  for mod in ${beta_modules[@]}; do
    if grep -q "$mod " go.mod; then
      go get -d "$mod"@"$VERSION"
    fi
  done
  for mod in ${rc_modules[@]}; do
    if grep -q "$mod " go.mod; then
      go get -d "$mod"@"$RC_VERSION"
    fi
  done
  for mod in ${stable_modules[@]}; do
    if grep -q "$mod " go.mod; then
      go get -d "$mod"@"$STABLE_VERSION"
    fi
  done
fi
